package com.hxz.djbxkj.HxzExecutor.Model;

import com.hxz.djbxkj.HxzExecutor.ExecutorCode.JobJk;
import com.hxz.djbxkj.HxzExecutor.ExecutorCode.Run.Abstract.AopJk;
import com.hxz.djbxkj.HxzExecutor.zy.RunType;

import java.util.HashMap;
import java.util.Map;

public class JobMode<T> {
    private int state= RunType.WQD;
    private StringBuffer errerMsg;//错误信息收集器

    //job-3要素
    private String name;//任务名称
    private String id;
    private ParameterMode parameter;
    private JobJk job;
    private AopJk aop;

    private Map<String,Object> data;

    private long begin=0l,end=0l;

    /**
     * 异步任务构造器
     * @param jk 异步任务实体（实现JobJk接口的业务对象）
     * @param pm 参数列表
     * @param id 异步任务函数id(被@HxzMethod注解修饰的id)
     * @param name 异步任务名称
     */
    public JobMode(JobJk jk, ParameterMode pm, String id,String name){
        this(jk,pm,id,name,null);
    }

    /**
     * 异步任务构造器
     * @param jk 异步任务实体（实现JobJk接口的业务对象）
     * @param pm 参数列表
     * @param id 异步任务函数id(被@HxzMethod注解修饰的id)
     * @param name 异步任务名称
     */
    public JobMode(JobJk jk, ParameterMode pm, String id,String name,AopJk aop){
        this.id=id;
        this.parameter=pm;
        this.job=jk;
        this.aop=aop;
        this.state= RunType.WQD;
        this.name=name;
        data=new HashMap<String, Object>();
        errerMsg=new StringBuffer();
    }

    public int getState() {
        return state;
    }

    public JobMode setState(int i){
        this.state=i;
        return this;
    }

    public Map<String, Object> getData() {
        return data;
    }

    public T getResult(){
        return (T)data.get("data");
    }

    public JobMode put(String k, Object v){
        this.data.put(k,v);
        return this;
    }

    public JobMode appendErrerMsg(String msg){
        errerMsg.append("["+this.name+"]错误信息：["+msg+"]\r\n");
        return this;
    }

    public String getMeg(){
        return errerMsg.toString();
    }

    public JobMode setBegin() {
        this.begin = System.currentTimeMillis();
        return this;
    }
    public JobMode setEnd() {
        this.end = System.currentTimeMillis();
        return this;
    }

    public JobMode setAop(AopJk aop) {
        this.aop = aop;
        return this;
    }

    public long getHs(){
        return this.end-this.begin;
    }

    public String getName() {
        return name;
    }

    public String getId() {
        return id;
    }

    public ParameterMode getParameter() {
        return parameter;
    }

    public JobJk getJob() {
        return job;
    }

    public AopJk getAop() {
        return aop;
    }

}
